home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / gmp-132.lha / gmp-1.3.2 / mpz_inp_str.c < prev    next >
C/C++ Source or Header  |  1993-05-06  |  2KB  |  106 lines

  1. /* mpz_inp_str(dest_integer, stream, base) -- Input a number in base
  2.    BASE from stdio stream STREAM and store the result in DEST_INTEGER.
  3.  
  4. Copyright (C) 1991, 1993 Free Software Foundation, Inc.
  5.  
  6. This file is part of the GNU MP Library.
  7.  
  8. The GNU MP Library is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; either version 2, or (at your option)
  11. any later version.
  12.  
  13. The GNU MP Library is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. GNU General Public License for more details.
  17.  
  18. You should have received a copy of the GNU General Public License
  19. along with the GNU MP Library; see the file COPYING.  If not, write to
  20. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  21.  
  22. #include <stdio.h>
  23. #include <ctype.h>
  24. #include "gmp.h"
  25. #include "gmp-impl.h"
  26.  
  27. static int
  28. char_ok_for_base (c, base)
  29.      int c;
  30.      int base;
  31. {
  32.   if (isdigit (c))
  33.     return (unsigned) c - '0' < base;
  34.   if (islower (c))
  35.     return (unsigned) c - 'a' + 10 < base;
  36.   if (isupper (c))
  37.     return (unsigned) c - 'A' + 10 < base;
  38.  
  39.   return 0;
  40. }
  41.  
  42. void
  43. #ifdef __STDC__
  44. mpz_inp_str (MP_INT *dest, FILE *stream, int base)
  45. #else
  46. mpz_inp_str (dest, stream, base)
  47.      MP_INT *dest;
  48.      FILE *stream;
  49.      int base;
  50. #endif
  51. {
  52.   char *str;
  53.   size_t str_size;
  54.   size_t i;
  55.   int c;
  56.   int negative = 0;
  57.  
  58.   str_size = 100;
  59.   str = (char *) (*_mp_allocate_func) (str_size);
  60.  
  61.   c = getc (stream);
  62.   if (c == '-')
  63.     {
  64.       negative = 1;
  65.       c = getc (stream);
  66.     }
  67.  
  68.   /* If BASE is 0, try to find out the base by looking at the initial
  69.      characters.  */
  70.   if (base == 0)
  71.     {
  72.       base = 10;
  73.       if (c == '0')
  74.     {
  75.       base = 8;
  76.       c = getc (stream);
  77.       if (c == 'x' || c == 'X')
  78.         {
  79.           base = 16;
  80.           c = getc (stream);
  81.         }
  82.     }
  83.     }
  84.  
  85.   for (i = 0; char_ok_for_base (c, base); i++)
  86.     {
  87.       if (i >= str_size)
  88.     {
  89.       size_t old_str_size = str_size;
  90.       str_size = str_size * 3 / 2;
  91.       str = (char *) (*_mp_reallocate_func) (str, old_str_size, str_size);
  92.     }
  93.       str[i] = c;
  94.       c = getc (stream);
  95.     }
  96.  
  97.   ungetc (c, stream);
  98.  
  99.   str[i] = 0;
  100.   _mpz_set_str (dest, str, base);
  101.   if (negative)
  102.     dest->size = -dest->size;
  103.  
  104.   (*_mp_free_func) (str, str_size);
  105. }
  106.